home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / InterLaunch 1.1.2 / src / util.c < prev   
Text File  |  1995-12-13  |  7KB  |  244 lines

  1. /* ----------------------------------------------------------------------
  2. util.c
  3. ---------------------------------------------------------------------- */
  4.  
  5. #include    "the_defines.h"
  6. #include    "the_globals.h"
  7. #include    "the_prototypes.h"
  8.  
  9. /* ----------------------------------------------------------------------
  10. LaunchIt
  11. ---------------------------------------------------------------------- */
  12. OSErr LaunchIt(FSSpec theApp, ProcessSerialNumber *serialNum)
  13. {
  14.     LaunchParamBlockRec        theLaunchParams;
  15.     OSErr                    errLaunching;
  16.  
  17.     theLaunchParams.launchBlockID = extendedBlock;
  18.     theLaunchParams.launchEPBLength = extendedBlockLen;
  19.     theLaunchParams.launchFileFlags = 0;
  20.     theLaunchParams.launchControlFlags = launchContinue + launchNoFileFlags;
  21.     theLaunchParams.launchAppSpec = &theApp;
  22.     theLaunchParams.launchAppParameters = nil;
  23.  
  24.     errLaunching = LaunchApplication(&theLaunchParams);
  25.     if (!errLaunching)
  26.         *serialNum = theLaunchParams.launchProcessSN;
  27.     return(errLaunching);
  28. }
  29.  
  30.  
  31. /* ----------------------------------------------------------------------
  32. OpenCP
  33. ---------------------------------------------------------------------- */
  34. OSErr OpenCP(FSSpec cdevSpec, OSType theCreator)
  35. {
  36.     short        i, vRefNum;
  37.     long        dirID;
  38.     long        gestaltAnswer;
  39.     long        found;
  40.     OSErr        result, myErr;
  41.     AppleEvent    aeEvent;    // event to create
  42.     AEDesc        myAddressDesc;
  43.     AEDesc        aeDirDesc;
  44.     AEDesc        listElem;
  45.     AEDesc        fileList;
  46.     FSSpec        dirSpec;
  47.     AliasHandle    dirAlias;
  48.     AliasHandle    fileAlias;
  49.     ProcessSerialNumber    process;
  50.  
  51.     result = ScheduleFinder();
  52.     if (result != noErr)
  53.         return(result);
  54.     result = Gestalt(gestaltAppleEventsAttr, &gestaltAnswer);
  55.     if (result != noErr)
  56.         return(result);
  57.     if (!BitTst(&gestaltAnswer,31 - gestaltAppleEventsPresent)) 
  58.         return(result);
  59.  
  60.     result = FindFolder(kOnSystemDisk, kControlPanelFolderType, kDontCreateFolder,
  61.                 &vRefNum, &dirID);
  62.     if (result != noErr)
  63.         return(result);
  64.  
  65.     if (FindAProcess(TYPE_FINDER, CREATOR_SYSTEM, &process))
  66.         return procNotFound;
  67.  
  68.     myErr = AECreateDesc(typeProcessSerialNumber, (Ptr) &process,
  69.                 sizeof(process), &myAddressDesc);
  70.     if (myErr) return myErr;
  71.  
  72.     myErr = AECreateAppleEvent(kAEFinderEvents, kAEOpenSelection,
  73.                 &myAddressDesc, kAutoGenerateReturnID, kAnyTransactionID,
  74.                 &aeEvent);
  75.     if (myErr) return myErr;
  76.  
  77.     FSMakeFSSpec(cdevSpec.vRefNum, cdevSpec.parID, nil, &dirSpec);
  78.     NewAlias(nil, &dirSpec, &dirAlias);
  79.     NewAlias(nil, &cdevSpec, &fileAlias);
  80.  
  81.     if (myErr = AECreateList(nil, 0, false, &fileList))
  82.         return myErr;
  83.  
  84.     HLock((Handle)dirAlias);
  85.     AECreateDesc(typeAlias, (Ptr) *dirAlias, GetHandleSize((Handle) dirAlias),
  86.                 &aeDirDesc);
  87.     HUnlock((Handle)dirAlias);
  88.     DisposHandle((Handle)dirAlias);
  89.  
  90.     if ((myErr = AEPutParamDesc(&aeEvent, keyDirectObject, &aeDirDesc)) ==
  91.                 noErr) {
  92.         AEDisposeDesc(&aeDirDesc);
  93.         HLock((Handle)fileAlias);
  94.  
  95.         AECreateDesc(typeAlias, (Ptr) *fileAlias, GetHandleSize((Handle)fileAlias),
  96.                 &listElem);
  97.         HUnlock((Handle)fileAlias);
  98.         DisposHandle((Handle)fileAlias);
  99.         myErr = AEPutDesc(&fileList, 0, &listElem);
  100.     }
  101.  
  102.     if (myErr) return myErr;
  103.     AEDisposeDesc(&listElem);
  104.  
  105.     if (myErr = AEPutParamDesc(&aeEvent, keySelection, &fileList))
  106.         return myErr;
  107.  
  108.     myErr = AEDisposeDesc(&fileList);
  109.  
  110.     myErr = AESend(&aeEvent, nil, kAENoReply+kAEAlwaysInteract+kAECanSwitchLayer,
  111.                 kAENormalPriority, kAEDefaultTimeout, nil, nil);
  112.  
  113.     AEDisposeDesc(&aeEvent);
  114.     
  115.     return myErr;
  116. }
  117.  
  118.  
  119. /* ----------------------------------------------------------------------
  120. FindAProcess
  121. ---------------------------------------------------------------------- */
  122. OSErr FindAProcess(OSType typeToFind, OSType creatorToFind, ProcessSerialNumberPtr processSN)
  123. {
  124.     ProcessInfoRec    tempInfo;
  125.     OSErr            myErr = noErr;
  126.     Str31            processName;
  127.     FSSpec            procSpec;
  128.     
  129.     processSN->lowLongOfPSN = kNoProcess;
  130.     processSN->highLongOfPSN = kNoProcess;
  131.  
  132.     tempInfo.processInfoLength = sizeof(ProcessInfoRec);
  133.     tempInfo.processName = (StringPtr)&processName;
  134.     tempInfo.processAppSpec = &procSpec;
  135.  
  136.     while ((tempInfo.processSignature != creatorToFind ||
  137.                 tempInfo.processType != typeToFind) ||
  138.                 myErr != noErr) {
  139.         myErr = GetNextProcess(processSN);
  140.         if (myErr ==noErr)
  141.             GetProcessInformation(processSN, &tempInfo);
  142.     }
  143.     return(myErr);
  144. }
  145.  
  146.  
  147. /* ----------------------------------------------------------------------
  148. ScheduleFinder
  149. ---------------------------------------------------------------------- */
  150. OSErr ScheduleFinder(void)
  151. {
  152.     ProcessSerialNumber finderPSN;          // the finder's psn
  153.     OSErr               myErr = noErr; 
  154.  
  155.     // Get the psn of the Finder
  156.     if( FindAProcess(TYPE_FINDER,CREATOR_SYSTEM,&finderPSN) )
  157.         return procNotFound;
  158.  
  159.     // Wake the Finder up
  160.     myErr = WakeUpProcess( &finderPSN );
  161.     if(myErr) return myErr;
  162.  
  163.     // Queue the Finder for activation
  164.     myErr = SetFrontProcess( &finderPSN );
  165.     if(myErr) return myErr;
  166.  
  167.     return myErr;
  168. }
  169.  
  170.  
  171. /* ----------------------------------------------------------------------
  172. QuitIt
  173. ---------------------------------------------------------------------- */
  174. OSErr QuitIt(ProcessSerialNumber *proc)
  175. {
  176.     OSErr err;
  177.     AEAddressDesc target;
  178.     AppleEvent theAE,aeReply;
  179.     
  180.     theAE.dataHandle = aeReply.dataHandle = target.dataHandle = nil;
  181.     
  182.     err = AECreateDesc(typeProcessSerialNumber,(Ptr)proc,sizeof(ProcessSerialNumber),&target);
  183.     if (err!=noErr)
  184.         return err;
  185.  
  186.     err = AECreateAppleEvent(kCoreEventClass,kAEQuitApplication,&target,
  187.                     kAutoGenerateReturnID,kAnyTransactionID,&theAE);
  188.     if (err!=noErr) {
  189.         AEDisposeDesc(&target);
  190.         return err;
  191.     }
  192.      
  193.     err = AESend(&theAE,&aeReply,kAENoReply,kAENormalPriority,kNoTimeOut,nil,nil);
  194.         
  195.     AEDisposeDesc(&target);
  196.     AEDisposeDesc(&theAE);
  197.     
  198.     return err;
  199. }
  200.  
  201.  
  202. /* ----------------------------------------------------------------------
  203. LocateFile
  204. ---------------------------------------------------------------------- */
  205. OSErr LocateFile (OSType fdType, OSType fdCreator, short vRefNum, FSSpec *spec)
  206. {
  207.     short            index;
  208.     CSParam            cs;
  209.     HFileInfo        spec1;
  210.     HFileInfo        spec2;
  211.     Ptr                buffer;
  212.     OSErr            err;
  213.  
  214.     buffer = NewPtr(8192);
  215.     if (MemError()) return;
  216.  
  217.     cs.ioNamePtr = NIL;
  218.     cs.ioVRefNum = vRefNum;
  219.     cs.ioMatchPtr = spec;
  220.     cs.ioReqMatchCount = 1;
  221.     cs.ioSearchBits = fsSBFlFndrInfo;
  222.     cs.ioSearchInfo1 = (CInfoPBPtr)&spec1;
  223.     cs.ioSearchInfo2 = (CInfoPBPtr)&spec2;
  224.     cs.ioSearchTime = 0;
  225.     cs.ioCatPosition.initialize = 0;
  226.     cs.ioOptBuffer = buffer;
  227.     cs.ioOptBufSize = 8192;
  228.     
  229.     spec1.ioFlFndrInfo.fdType = fdType;
  230.     spec1.ioFlFndrInfo.fdCreator = fdCreator;
  231.     spec1.ioFlFndrInfo.fdFlags = 0;
  232.     *(long *)&spec1.ioFlFndrInfo.fdLocation = 0L;
  233.     spec1.ioFlFndrInfo.fdFldr = 0;
  234.  
  235.     spec2.ioFlFndrInfo.fdType = (ResType)0xffffffff;
  236.     spec2.ioFlFndrInfo.fdCreator = (ResType)0xffffffff;
  237.     spec2.ioFlFndrInfo.fdFlags = 0x4000;
  238.     *(long *)&spec2.ioFlFndrInfo.fdLocation = 0;
  239.     spec2.ioFlFndrInfo.fdFldr = 0;
  240.  
  241.     DisposePtr(buffer);    
  242.     return(PBCatSearch(&cs,FALSE));
  243. }
  244.